import AppwrightGestures from '../../e2e/framework/AppwrightGestures';
import AppwrightSelectors from '../../e2e/framework/AppwrightSelectors';
import { SWAP_SCREEN_DESTINATION_TOKEN_INPUT_ID, SWAP_SCREEN_QUOTE_DISPLAYED_ID, SWAP_SCREEN_SOURCE_TOKEN_INPUT_ID } from './testIDs/Screens/SwapScreen.testIds';
import { expect as appwrightExpect } from 'appwright';
import { PerpsWithdrawViewSelectorsIDs } from '../../e2e/selectors/Perps/Perps.selectors';
import { QuoteViewSelectorText } from '../../e2e/selectors/swaps/QuoteView.selectors';
import Selectors from '../helpers/Selectors.js';
import { LoginViewSelectors } from '../../e2e/selectors/wallet/LoginView.selectors';
import { splitAmountIntoDigits } from 'appwright/utils/Utils.js';

class BridgeScreen {

  get device() {
    return this._device;
  }

  set device(device) {
    this._device = device;

  }
  get sourceTokenInput() {
    return AppwrightSelectors.getElementByID(this._device, SWAP_SCREEN_SOURCE_TOKEN_INPUT_ID);
  }

  get destTokenInput() {
    return AppwrightSelectors.getElementByID(this._device, SWAP_SCREEN_DESTINATION_TOKEN_INPUT_ID);
  }

  get quoteDisplayed() {
    return AppwrightSelectors.getElementByID(this._device, SWAP_SCREEN_QUOTE_DISPLAYED_ID);
  }
  get destinationTokenArea(){
    return AppwrightSelectors.getElementByID(this._device, PerpsWithdrawViewSelectorsIDs.DEST_TOKEN_AREA);
    
  }
  get seeAllDropDown(){
    return AppwrightSelectors.getElementByText(this._device, "See all");

  }

  getNetworkButton(networkName) {
    if (!this._device) {
      return Selectors.getXpathElementByResourceId(
        LoginViewSelectors.PASSWORD_INPUT,
      );
    } else {
      if (AppwrightSelectors.isAndroid(this._device)) {
        return AppwrightSelectors.getElementByXpath(this._device, `//*[@content-desc="${networkName}"]`);
      } else {
        return AppwrightSelectors.getElementByID(this._device, `${networkName}`);
      }
    }
  }

  get getETHQuotesButton(){
    return AppwrightSelectors.getElementByText(this._device, QuoteViewSelectorText.GET_QUOTES);
  }

  async isQuoteDisplayed() {
      const mmFee = await AppwrightSelectors.getElementByCatchAll(this._device, "Includes 0.875% MM fee");
      await appwrightExpect(mmFee).toBeVisible({ timeout: 30000 });
    

  }

  async enterSourceTokenAmount(amount) {
    // Split amount into digits
    const digits = splitAmountIntoDigits(amount);
    console.log('Amount digits:', digits);
    for (const digit of digits) {
      if (AppwrightSelectors.isAndroid(this._device)) {
        if (digit != '.') {
          const numberKey = await AppwrightSelectors.getElementByXpath(this._device, `//android.widget.Button[@content-desc='${digit}']`)
          await appwrightExpect(numberKey).toBeVisible({ timeout: 30000 });
          await AppwrightGestures.tap(numberKey);
        }
        else {
          const numberKey = await AppwrightSelectors.getElementByXpath(this._device, `//android.view.View[@text="."]`);
          await appwrightExpect(numberKey).toBeVisible({ timeout: 30000 });
          await AppwrightGestures.tap(numberKey);
        }
      }
      else {
        const numberKey = await AppwrightSelectors.getElementByXpath(this._device, `//XCUIElementTypeButton[@name="${digit}"]`);
        await appwrightExpect(numberKey).toBeVisible({ timeout: 30000 });
        await AppwrightGestures.tap(numberKey);
      }
    }
  }

  async selectNetworkAndTokenTo(network, token) {
    const destinationToken = await this.destinationTokenArea;
    await AppwrightGestures.tap(destinationToken);
    const filterNetworkButton = await AppwrightSelectors.getElementByCatchAll(this._device, 'See all');
    await AppwrightGestures.tap(filterNetworkButton);
    const networkButton = await this.getNetworkButton(network);
    await AppwrightGestures.tap(networkButton);
    let tokenNetworkId;
    if (network == 'Ethereum'){
      tokenNetworkId = `0x1`;
    }
    else if (network == 'Polygon'){
      tokenNetworkId = `0x89`;
    }
    else if (network == 'Solana'){
      tokenNetworkId = `solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp`;
    }
    const tokenButton = await AppwrightSelectors.getElementByID(this._device, `asset-${tokenNetworkId}-${token}`);
    await AppwrightGestures.tap(tokenButton);
  }

  async tapGetQuotes(network){
    if (network == 'Ethereum'){
    const quotesButton = this.getETHQuotesButton;
    await appwrightExpect(quotesButton).toBeVisible({ timeout: 10000 });
    await AppwrightGestures.tap(quotesButton);
    }
  }

  async enterDestinationTokenAmount(amount) {
    const element = this.destTokenInput;
    await AppwrightGestures.typeText(element, amount);
  }

  async isVisible() {
    const element = await this.sourceTokenInput;
    await appwrightExpect(element).toBeVisible({ timeout: 10000 });
  }
}

export default new BridgeScreen();
